package com.ai.emall.utils.util;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ai.emall.utils.exception.SystemException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class GnExceptionCodeUtil {
    
    public static void main(String[] agrs){
        if(true){
            throw new SystemException("(CUST_ID=123)");
        }
    }
    
    /**
     * 转换错误信息
     * @param errCode
     * @return
     */
    public static String convertError(String errCode){ 
        String message  = convertError(errCode,"");
        return message;
     }
    
    /**
     * 转换错误信息
     * @param errCode
     * @param keyinfo
     * @return
     */
    public static String convertError(String errCode,String keyinfo){
       String[] keyinfos = new String[]{keyinfo==null?"":keyinfo};
       String message  = convertError(errCode,keyinfos);
       return message;
    }
    
    /**
     * 转换错误信息
     * @param errCode
     * @param keyinfos
     * @return
     */
    public static String convertError(String errCode,String[] keyinfos){
        String template = getExcepTemplate(errCode);
        Map<Integer,String> p =new LinkedHashMap<Integer,String>();
        if(!CollectionUtil.isEmpty(keyinfos)){
            int i=0;
            for(String key:keyinfos){
                p.put(Integer.valueOf(i), key);
                i++;
            }
        }
        ExpressionAnalyzer any = new ExpressionAnalyzer(template,p);
        String message = any.analy();
        return message; 
    }

    private static String getExcepTemplate(String errorCode) {
        errorCode = StringUtil.toString(errorCode);
        JSONObject jsonObject = getRedisData();
        if (jsonObject != null) {
            JSONObject json = jsonObject.getJSONObject(errorCode);
            String template =json!=null? json.getString("exceptionTemplate"):null;
            return template;
        }
        return null;
    }

    private static JSONObject getRedisData() {
    	//TODO 增加从缓存读取数据
        /*Object cacheJsonData = CacheFactory.getData(CacheConstants.CacheKey.GN_EXCEPTION_CODE);
        String cacheStringData = StringUtil.toString(cacheJsonData);
        if (!StringUtil.isBlank(cacheStringData)) {
            JSONObject data = JSON.parseObject(cacheStringData);
            return data;
        }*/
        return new JSONObject();
    } 
    
    private static class ExpressionAnalyzer {

        // 表达式
        private String expression;

        private Map<Integer, String> p;

        // 表达式的特殊变量正则表达式 如{0} {1}
        private static final String REGEX = "(\\{.+?\\})";

        private ExpressionAnalyzer(String expression, Map<Integer, String> p) {
            this.expression = expression==null?"":expression;
            this.p = p;
        }

        public String analy() {
            Map<Integer, String> expMap = this.match();
            for (Integer index : expMap.keySet()) {
                String exp = expMap.get(index);
                String value = p.get(index);
                expression = expression.replace(exp, StringUtil.toString(value));
            }
            return expression;
        }

        private Map<Integer, String> match() {
            /* 1.表达式解析结果 */
            Map<Integer, String> expMap = new LinkedHashMap<Integer, String>();
            /* 2.采用指定的正则表达式进行匹配查找 */
            Pattern p = Pattern.compile(REGEX);
            Matcher m = p.matcher(expression);
            int index = 0;
            while (m.find()) {
                String group = m.group();
                if (!StringUtil.isBlank(group)) {
                    group = group.trim();
                    if (group.length() > 2) {
                        // String index = group.substring(1, group.length() - 1);
                        expMap.put(index, group);
                        index++;
                    }
                }
            }
            return expMap;
        }
    }

}
